<template>
  <div id="board"></div>
</template>

<script>
import * as THREE from "three";
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls.js";

export default {
  data() {
    return {
      scene: null,
      camera: null,
      renderer: null,
      cube: null,
      control: null,
    };
  },
  methods: {
    init() {
      this.scene = new THREE.Scene();

      var k = 800 / 600; //窗口宽高比
      var s = 200; //三维场景显示范围控制系数，系数越大，显示的范围越大
      //创建相机对象
      this.camera = new THREE.OrthographicCamera(-s * k, s * k, s, -s, 1, 1000);
      this.camera.position.set(200, 300, 200); //设置相机位置
      this.camera.lookAt(this.scene.position); //设置相机方向(指向的场景对象)
      // this.camera = new THREE.PerspectiveCamera(75, 400 / 400, 0.1, 1000);
      // this.camera.position.z = 20;
      this.renderer = new THREE.WebGLRenderer();
      this.renderer.setSize(800, 600);
      this.renderer.setClearColor(0xb9d3ff, 1); //设置背景颜色
      document.getElementById("board").appendChild(this.renderer.domElement);
    },
    initCube() {
      // var geometry = new THREE.BoxGeometry(4, 2, 1);
      // //var geometry = new THREE.SphereGeometry( 5, 32, 32);
      // var material = new THREE.MeshBasicMaterial( { color: 0xffff00 } );
      // //var material = new THREE.MeshDepthMaterial();
      // this.cube = new THREE.Mesh( geometry, material );
      // this.control = new OrbitControls( this.camera, this.renderer.domElement );
      // this.scene.add( this.cube );

      /**
       * 创建网格模型
       */
      // var geometry = new THREE.SphereGeometry(60, 40, 40); //创建一个球体几何对象
      var geometry = new THREE.BoxGeometry(100, 100, 100); //创建一个立方体几何对象Geometry
      var material = new THREE.MeshLambertMaterial({
        color: 0x0000ff,
      }); //材质对象Material
      var mesh = new THREE.Mesh(geometry, material); //网格模型对象Mesh
      this.scene.add(mesh); //网格模型添加到场景中

      // 球体网格模型
      var geometry2 = new THREE.SphereGeometry(60, 40, 40);
      var material2 = new THREE.MeshLambertMaterial({
        color: 0xff00ff,
      });
      var mesh2 = new THREE.Mesh(geometry2, material2); //网格模型对象Mesh
      mesh2.translateY(120); //球体网格模型沿Y轴正方向平移120
      this.scene.add(mesh2);

      // 圆柱网格模型
      var geometry3 = new THREE.CylinderGeometry(50, 50, 100, 25);
      var material3 = new THREE.MeshLambertMaterial({
        color: 0xffff00,
      });
      var mesh3 = new THREE.Mesh(geometry3, material3); //网格模型对象Mesh
      // mesh3.translateX(120); //球体网格模型沿Y轴正方向平移120
      mesh3.position.set(120, 0, 0); //设置mesh3模型对象的xyz坐标为120,0,0
      this.scene.add(mesh3); //

      // 辅助坐标系  参数250表示坐标系大小，可以根据场景大小去设置
      var axisHelper = new THREE.AxisHelper(250);
      this.scene.add(axisHelper);

      /**
       * 光源设置
       */
      //点光源
      var point = new THREE.PointLight(0xffffff);
      point.position.set(400, 200, 300); //点光源位置
      this.scene.add(point); //点光源添加到场景中
      //环境光
      var ambient = new THREE.AmbientLight(0x444444);
      this.scene.add(ambient);

      this.control = new OrbitControls(this.camera, this.renderer.domElement);
    },
    animate() {
      requestAnimationFrame(this.animate);
      // this.cube.rotation.x += 0.01;
      // this.cube.rotation.y += 0.01;
      this.control.update();
      this.renderer.render(this.scene, this.camera);
    },
  },
  mounted() {
    this.init();
    this.initCube();
    this.animate();
  },
};
</script>

<style scoped>
#board {
  width: 400px;
  height: 400px;
  margin: 0 auto;
}
</style>